<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件


/**传入手机号和验证码，暂未采用批量验证的方法，手机号用逗号间隔即可批量发送，但验证码不变
 * @param int $phonenumber
 * @param $captcha
 * @return mixed|string
 */
function SMS(string $phonenumber, $captcha)
{
    $url = "http://smssh1.253.com/msg/send/json";
//    $captcha = rand(1000, 9999);
    $postFields['account'] = 'N1372254';
    $postFields['password'] = 'OF6Ivu54ir637c';
    $postFields['phone'] = $phonenumber;
    $postFields['msg'] = urlencode('【内蒙古食品安全网站】验证码测试：' . $captcha);
    $postFields['report'] = true;
    $postFields = json_encode($postFields);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json; charset=utf-8'
        )
    );
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
    curl_setopt($ch, CURLOPT_TIMEOUT, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    $ret = curl_exec($ch);
    if (false == $ret) {
        $result = curl_error($ch);
    } else {
        $rsp = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        if (200 != $rsp) {
            $result = "请求状态 " . $rsp . " " . curl_error($ch);
        } else {
            $result = $ret;
        }
    }
    curl_close($ch);
    return $result;

}

function validateCompany($company_name,$company_number,$company_address)
{
    $company_name=urlencode($company_name);
    $host = "http://jisuqygsxx.market.alicloudapi.com";
    $path = "/enterprise/query";
    $method = "GET";
    $appcode = "5c399b8a34e84de2ad43444ad82da466";
    $headers = array();
    array_push($headers, "Authorization:APPCODE " . $appcode);
    $querys = "company=".$company_name;
    $bodys = "";
    $url = $host . $path . "?" . $querys;

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curl, CURLOPT_FAILONERROR, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HEADER, 0);
    if (1 == strpos("$".$host, "https://"))
    {
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    }
    $result=curl_exec($curl);
    $result=json_decode($result);
//    dump($result);
    if ($result->status!=0){
        return 'failed';
    }else{
        $basic=$result->result->basic;
        if ($company_number==$basic->creditno && $company_address==$basic->regaddress){
            return 'success';
        }else{
            return 'wrong';
        }
    }



}

/**
 *
 */
function validateIDnumber(string $id_number, $name)
{
    $host = "http://idcard.market.alicloudapi.com";
    $path = "/lianzhuo/idcard";
    $method = "GET";
    $appcode = "5c399b8a34e84de2ad43444ad82da466";
//    $id_number='120111199506083513';
//    $name=urlencode('栗禄尧');
    $headers = array();
    array_push($headers, "Authorization:APPCODE " . $appcode);
    $querys = "cardno=" . $id_number . "&name=" . $name;
//    $bodys = "";
    $url = $host . $path . "?" . $querys;

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curl, CURLOPT_FAILONERROR, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HEADER, 0);
    if (1 == strpos("$" . $host, "https://")) {
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    }
    $output = curl_exec($curl);

    return $output;
}
